****************************************************************************************
*This dofile aggregate monthly temperature, precipitation, and alternative drought measures by year, season, and crop
*****************************************************************************************

cd "$root"
clear all

foreach crop in c_ s_ {
	clear
	use "data\temp\weather_month.dta" 

	merge 1:1 fips year month using "data\temp\alt_drought_long.dta"
	drop _merge
	drop d*
	gen ml = 30
	replace ml = 31 if month == 1 | month == 3 | month == 5 | month == 7 | month == 8 | month == 10 | month == 12
	replace ml = 28 if month == 2
	replace ml = 29 if year/4 == trunc(year/4) & month == 2

	egen normal_t = rowtotal(tbinminus1c - tbin38c)

	gen tbinminus = 0
	replace tbinminus = ml -normal_t if month < 4 | month > 10
	replace tbinminus = 0 if tbinminus < 0

	gen tbinplus = 0
	replace tbinplus = ml - normal_t if month >= 4 & month <= 10
	replace tbinplus = 0 if tbinplus < 0

	tostring year month ml, replace
	generate start = date(month + "/1/" + year, "MDY")
	format %td start
	generate end = date(month + "/" + ml + "/" + year, "MDY")
	format %td end
	destring ml, replace

	gen st_fips = trunc(fips/1000)


	merge m:1 st_fips using "data/raw/`crop'dates.dta"
	keep if _merge == 3

	forvalues yr = 2001(1)2023 {

		* Define season starting/end points  
		gen `crop'dpa_begin`yr' = date(`crop'pa_begin_m + "/" + `crop'pa_begin_d + "/`yr'", "MDY")
		local pyr = `yr' - 1
		format %td `crop'dpa_begin`yr'

		gen `crop'dpa_end`yr' = date(`crop'pa_end_m + "/" + `crop'pa_end_d + "/`yr'", "MDY")
		format %td `crop'dpa_end`yr'

		gen `crop'dha_end1`yr' = date(`crop'ha_end_m + "/" + `crop'ha_end_d + "/`pyr'", "MDY")
		format %td `crop'dha_end1`yr'

		gen `crop'dha_end2`yr' = date(`crop'ha_end_m + "/" + `crop'ha_end_d + "/`yr'", "MDY")
		format %td `crop'dha_end2`yr'


		* Days in pre-season for each year
		gen temp1 = max(start, `crop'dha_end1`yr' + 1)
		format %td temp1

		gen temp2 = min(end, `crop'dpa_begin`yr' - 1)
		format %td temp2


		gen `crop'pre_`yr' = 0
		replace `crop'pre_`yr' = temp2 - temp1 + 1
		replace `crop'pre_`yr' = 0 if end <= `crop'dha_end1`yr' | start > `crop'dpa_begin`yr'
		drop temp* 

		* Total pre-season length
		gen `crop'sl_b`yr' = `crop'dpa_begin`yr' - 1 - `crop'dha_end1`yr'

		* Days in planting season for each year
		gen temp1 = max(start, `crop'dpa_begin`yr')
		format %td temp1

		gen temp2 = min(end, `crop'dpa_end`yr')
		format %td temp2

		gen `crop'planting_`yr' = 0
		replace `crop'planting_`yr' = temp2 - temp1 + 1
		replace `crop'planting_`yr' = 0 if end <= `crop'dpa_begin`yr' | start > `crop'dpa_end`yr'
		drop temp* 

		* Total planting-season length
		gen `crop'sl_p`yr' = `crop'dpa_end`yr' - `crop'dpa_begin`yr' + 1

		* Days in growing season for each year 
		gen temp1 = max(start, `crop'dpa_end`yr' + 1)
		format %td temp1

		gen temp2 = min(end, `crop'dha_end2`yr')
		format %td temp2

		gen `crop'growing_`yr' = 0
		replace `crop'growing_`yr' = temp2 - temp1 + 1
		replace `crop'growing_`yr' = 0 if end <= `crop'dpa_end`yr' | start > `crop'dha_end2`yr'
		drop temp* 

		* Total growing-season length
		gen `crop'sl_g`yr' = `crop'dha_end2`yr' - `crop'dpa_end`yr'

		local weather tbinminus tbinminus1c tbin2c tbin5c tbin8c tbin11c tbin14c tbin17c tbin20c tbin23c tbin26c tbin29c tbin32c tbin35c tbin38c tbinplus prec 
		 
		foreach x of local weather {		
			gen `crop'`x'_b`yr' = `x'*`crop'pre_`yr'/ml
			gen `crop'`x'_p`yr' = `x'*`crop'planting_`yr'/ml
			gen `crop'`x'_g`yr' = `x'*`crop'growing_`yr'/ml
		}

		local drought pmdi phdi pdsi pmdi_w phdi_w pdsi_w
		foreach x of local drought {
			gen `crop'`x'_b`yr' = `x'*`crop'pre_`yr'/`crop'sl_b`yr'
			gen `crop'`x'_p`yr' = `x'*`crop'planting_`yr'/`crop'sl_p`yr'
			gen `crop'`x'_g`yr' = `x'*`crop'growing_`yr'/`crop'sl_g`yr'
		}
	}

	collapse `crop'sl_* (rawsum) `crop'pmdi_* `crop'phdi_* `crop'pdsi_*  `crop'tbin* `crop'prec*, by(fips)
	reshape long  `crop'pmdi_b `crop'phdi_b `crop'pdsi_b ///
	`crop'pmdi_p `crop'phdi_p `crop'pdsi_p ///
	`crop'pmdi_g `crop'phdi_g `crop'pdsi_g ///
	`crop'pmdi_w_b `crop'phdi_w_b `crop'pdsi_w_b ///
	`crop'pmdi_w_p `crop'phdi_w_p `crop'pdsi_w_p ///
	`crop'pmdi_w_g `crop'phdi_w_g `crop'pdsi_w_g ///
	`crop'tbinminus_p `crop'tbinplus_p `crop'tbinminus1c_p `crop'tbin2c_p `crop'tbin5c_p `crop'tbin8c_p `crop'tbin11c_p ///
	`crop'tbin14c_p `crop'tbin17c_p `crop'tbin20c_p `crop'tbin23c_p `crop'tbin26c_p `crop'tbin29c_p ///
	`crop'tbin32c_p `crop'tbin35c_p `crop'tbin38c_p `crop'prec_p  `crop'tbinminus_g `crop'tbinplus_g `crop'tbinminus1c_g ///
	`crop'tbin2c_g `crop'tbin5c_g `crop'tbin8c_g `crop'tbin11c_g `crop'tbin14c_g `crop'tbin17c_g ///
	`crop'tbin20c_g `crop'tbin23c_g `crop'tbin26c_g `crop'tbin29c_g `crop'tbin32c_g `crop'tbin35c_g `crop'tbin38c_g `crop'prec_g ///
	`crop'tbinminus_b `crop'tbinplus_b  `crop'tbinminus1c_b `crop'tbin2c_b `crop'tbin5c_b ///
	`crop'tbin8c_b `crop'tbin11c_b `crop'tbin14c_b `crop'tbin17c_b `crop'tbin20c_b `crop'tbin23c_b ///
	`crop'tbin26c_b `crop'tbin29c_b `crop'tbin32c_b `crop'tbin35c_b `crop'tbin38c_b `crop'prec_b ///
	`crop'sl_b `crop'sl_p `crop'sl_g, i(fips) j(year)

	save data/temp/`crop'weather_altdrought.dta, replace
}